Completed
Push — master ( 60f354...0d0dec )
by hung
11s
created

input.js ➔ ???   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 4
dl 0
loc 23
rs 8.7972
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A input.js ➔ ... ➔ ??? 0 3 1
1
const fs = require('fs')
2
const { join } = require('path')
3
const { Readable } = require('stream')
4
5
module.exports = (input, callback) => {
6
  if (typeof input === 'string') {
7
    // Input as file
8
    const path = join(__dirname, '../', input)
9
    if (!fs.existsSync(path)) {
10
      console.error(`Cannot read file ${path}`)
11
      return
12
    }
13
    const html = fs.readFileSync(path, 'utf8')
14
    callback(html)
15
  } else if (input instanceof Readable) {
16
    // Input as stream
17
    input.on('data', chunk => {
18
      callback(chunk.toString())
19
    })
20
    input.on('error', err => {
21
      console.error(err)
22
    })
23
  } else {
24
    // Unknown input
25
    console.error('Invalid Input')
26
  }
27
}
28